package com.ostrich.note.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;


import com.gyf.immersionbar.ImmersionBar;
import com.ostrich.note.R;
import com.ostrich.note.event.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;


public abstract class BaseActivity<VDB extends ViewDataBinding> extends AppCompatActivity {
   protected VDB binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ImmersionBar
                .with(this)
                .fitsSystemWindows(true)//解决状态栏和布局重叠问题，任选其一
                .statusBarColor(R.color.pink)//状态栏颜色，不写默认透明色
                .init();
        binding = DataBindingUtil.setContentView(this,getLayout());
        iniView();
        initData();
        initListener();
        //注册广播
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receive(Event event){
        //判断是否为自己发送的广播
        if (event.target.equals(getClass().getSimpleName())){
            onEvent(event);
        }
    }
    //子类重写该方法就可以处理接受数据，不要在base里面做具体的处理
    public void onEvent(Event event){}
    protected abstract void iniView();

//    public abstract int getStatusBarColor();
   public abstract int getLayout();
   public abstract void initData();
   public abstract void initListener();

   public void startActivity(Class tClass){
       Intent intent = new Intent(this, tClass);
       startActivity(intent);
   }

    public void startActivityBundle(Class tClass,Bundle bundle){
        Intent intent = new Intent(this, tClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    public Bundle getBundle(){
       return getIntent().getExtras();
    }




}
